package com.service.schedule.tm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.dao.sys.MessageDao;
import com.dao.sys.SysDao;
import com.dao.tm.PlanDao;
import com.enums.SaleOrderTypeEnum;
import com.model.sys.MessageModel;
import com.model.tm.PlanModel;
import com.service.external.sap.CallSapRfcService;

@Service("planschduleService")
public class PlanSchduleService {
	static final Logger logger = LoggerFactory.getLogger(PlanSchduleService.class);

	@Autowired
	PlanDao planDao;
	@Autowired
	MessageDao messageDao;
	@Autowired
	SysDao sysDao;
	@Autowired
	CallSapRfcService callSapRfcService;

	/**
	 * 审核超时默认通过
	 */
	public void insertOverTime() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("status", Global.NO + "");
		hashMap.put("planServiceStatus", Global.YES + "");
		hashMap.put("sendDate", DateUtils.addDays(DateUtils.getCurrentDate(), 1));
		List<PlanModel> messageModels = planDao.selectList(hashMap);
		List<PlanModel> successList = new ArrayList<PlanModel>();
		for (PlanModel planModel : messageModels) {
			// 全部通过时，有效，否则未生效
			if (planModel.getPlanProdStatus() != null || planModel.getPlanStorStatus() != null
					|| planModel.getPlanTmStatus() != null) {
//				continue;
			}
			// 预装不推送sap
			if (!planModel.getOrdCode().equals(SaleOrderTypeEnum.yz.getValue())
					&& !planModel.getOrdCode().equals(SaleOrderTypeEnum.jj.getValue())) {
				successList.add(planModel);
			}
			planModel.setStatus(Global.YES + "");
			planModel.setPlanProdStatus(Global.YES + "");
			planModel.setPlanStorStatus(Global.YES + "");
			planModel.setPlanTmStatus(Global.YES + "");
			successList.add(planModel);
			planDao.update(planModel);
		}
		// 推送sap
		if (successList.size() > 0) {
			HttpResult rfcHttpResult = callSapRfcService.createPlan(successList);
		}
	}

	/**
	 * 加急超时两小时默认通过
	 */
	public void insertYJOverTime() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("status", Global.NO + "");
		hashMap.put("planServiceStatus", Global.YES + "");
		hashMap.put("sendDate", DateUtils.addDays(DateUtils.getCurrentDate(), 1));
		List<PlanModel> messageModels = planDao.selectList(hashMap);
		List<PlanModel> successList = new ArrayList<PlanModel>();
		for (PlanModel planModel : messageModels) {
			// 全部通过时，有效，否则未生效
			if (planModel.getPlanProdStatus() != null || planModel.getPlanStorStatus() != null
					|| planModel.getPlanTmStatus() != null) {
				continue;
			}
			if (planModel.getOrdCode().compareTo(SaleOrderTypeEnum.jj.getValue()) <= 0) {
				continue;
			}
			if (DateUtils.getTimeString(System.currentTimeMillis() - 1000 * 60 * 60 * 2)
					.compareTo(planModel.getCreateTime()) <= 0) {
				continue;
			}
			// 预装不推送sap
			if (!planModel.getOrdCode().equals(SaleOrderTypeEnum.yz.getValue())) {
				planModel.setStatus(Global.YES + "");
				planModel.setPlanProdStatus(Global.YES + "");
				planModel.setPlanStorStatus(Global.YES + "");
				planModel.setPlanTmStatus(Global.YES + "");
				successList.add(planModel);
//				planDao.update(planModel);
			}

		}
		// 推送sap
		if (successList.size() > 0) {
//			HttpResult rfcHttpResult = callSapRfcService.createPlan(successList);

		}
	}

	public void insertMsg(List<PlanModel> planModel, String userEnum) {
		List<HashMap<String, Object>> serviceHashMaps = sysDao.queryItemByRoleName(userEnum);
		for (HashMap<String, Object> serviceHashMap : serviceHashMaps) {
			MessageModel messageModel = new MessageModel();
			messageModel.setMsgId(UUID.randomUUID().toString());
			messageModel.setMsgReceiveId(serviceHashMap.get("wx").toString());
			messageModel.setMsgReceiveName(serviceHashMap.get("username").toString());
			messageModel.setMsgContent("今日未完成审批排货计划条数：" + planModel.size());
			messageModel.setMsgType(com.enums.MsgEnum.qywx.getValue());
			messageModel.setStatus(Global.NO + "");
			messageModel.setCreateTime(DateUtils.getCurrentTime());
			messageDao.insert(messageModel);
		}
	}

	/**
	 * 记录日志
	 * 
	 * @param planModel
	 * @param userEnum
	 */
	public void insertMsg(PlanModel planModel, String userEnum) {
		List<HashMap<String, Object>> serviceHashMaps = sysDao.queryItemByRoleName(userEnum);
		for (HashMap<String, Object> serviceHashMap : serviceHashMaps) {
			MessageModel messageModel = new MessageModel();
			messageModel.setMsgId(UUID.randomUUID().toString());
			messageModel.setMsgReceiveId(serviceHashMap.get("wx").toString());
			messageModel.setMsgReceiveName(serviceHashMap.get("username").toString());
			messageModel.setMsgContent("今日未完成审批排货计划：" + planModel.getPlanName());
			messageModel.setMsgType(com.enums.MsgEnum.qywx.getValue());
			messageModel.setStatus(Global.NO + "");
			messageModel.setCreateTime(DateUtils.getCurrentTime());
			messageDao.insert(messageModel);
		}
	}

}
